package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.CheckInformationAdapter;
import com.example.lanouhn.firstapp.activity.model.CartList;
import com.example.lanouhn.firstapp.activity.utils.CartListJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

import java.util.List;

public class CheckInformationActivity extends AppCompatActivity {
    //新地址列表
    private RelativeLayout rl_address_information;
    private TextView tv_information ;

    //用户登录标识
    private String key;
    private String goods_id;
    private RecyclerView recyclerView;
    //数据集合
    private List<CartList> dataList;
    private CheckInformationAdapter adapter;

    //订单总金额
    private TextView tv_total_amount;

    //返回按钮
    private ImageView iv_back;
    //确认订单
    private TextView confirmOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_information);
        key = (String) SPUtils.get(CheckInformationActivity.this,"log_key","");

        initView();
        //请求网络数据
        getDataFromWeb();

        //使用新地址监听事件
        tv_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rl_address_information.setVisibility(View.VISIBLE);
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        confirmOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CheckInformationActivity.this,MyOrdersActivity.class);
                startActivityForResult(intent,100);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    break;
                case 1:
                    //创建adapter
                    adapter  = new CheckInformationAdapter(CheckInformationActivity.this,dataList);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    //recyclerView的item的点击事件
                    adapter.setCartListRecyclerViewListener(new CheckInformationAdapter.CartListRecyclerViewListener() {
                        @Override
                        public void onclick(View view, int position) {
                            //通过传送id 跳转到商品详情页面
                            Intent intent = new Intent(CheckInformationActivity.this, GoodsDetailsActivity.class);
                            intent.putExtra("goods_id", dataList.get(position).getGoods_id());
                            startActivity(intent);
                        }
                    });

                    break;
                default:
                    break;


            }


        }
    };
    private void getDataFromWeb() {
        //开启线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://www.niaolema.com/mobile/index.php?act=member_cart&op=cart_list";
                String params = "key=" + key;
                String result = HttpUtils.doPost(url,params);
                if(!result.equals("") && !result.equals("null")){
                    dataList = CartListJson.getCartList(result);

                    if(dataList.size()>0){
                        //解析成功
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }


                }else {
                    handler.sendEmptyMessage(0);

                }


            }
        }).start();



    }

    private void initView() {

        tv_information = (TextView) findViewById(R.id.tv_information);
        rl_address_information = (RelativeLayout) findViewById(R.id.rl_address_information);
        tv_total_amount = (TextView) findViewById(R.id.tv_total_amount);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(gridLayoutManager);

        iv_back = (ImageView) findViewById(R.id.iv_back);

        confirmOrder = (TextView) findViewById(R.id.confirmOrder);

    }
}
